package com.lmx.healthcode.common.base;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lmx.healthcode.R;
import com.lmx.healthcode.util.KeyboardUtil;
import com.lmx.healthcode.util.ToastUtil;


/**
 * @author Leung
 * @date 2020/9/10 23:56
 */
public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());

        initView();
        setOnBackClickListener();
        initListener();
        initData();
    }

    /**
     * 获取布局文件id
     *
     * @return 布局文件id
     */
    public abstract int getLayoutId();

    /**
     * 初始化视图
     */
    public abstract void initView();

    /**
     * 设置关闭按钮的点击事件
     */
    private void setOnBackClickListener() {
        ImageView imgClose = findViewById(R.id.img_close);
        imgClose.setOnClickListener(this::setOnBackClickListener);
    }

    public void setOnBackClickListener(View view) {
        finish();
    }

    /**
     * 初始化监听器
     */
    public abstract void initListener();

    /**
     * 初始化数据
     */
    public abstract void initData();

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
            View view = this.getCurrentFocus();
            if (view instanceof EditText) {
                KeyboardUtil.closeKeyboard(this.getCurrentFocus());
            }
        return super.dispatchTouchEvent(ev);
    }
}
